﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Advance.Repositories.EntityFrameworkCore;
using Advance.Repositories.EntityFrameworkCore.Models;
using IdentityServer4.EntityFramework.DbContexts;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace Advance.Mvc
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args)
                .MigrateDbContext<ApplicationDbContext>((ctx, services) =>
                {
                    new ApplicationDbContextSeed().SeedAsync(ctx, services)
                    .Wait();
                })
                 .MigrateDbContext<ConfigurationDbContext>((ctx, services) =>
                 {
                     new ConfigurationDbContextSeed().SeedAsync(ctx, services)
                     .Wait();
                 })
                  .MigrateDbContext<PersistedGrantDbContext>((ctx, services) =>
                  {
                      new PersistedGrantDbContextSeed().SeedAsync(ctx, services)
                      .Wait();
                  })
                .Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration(opt =>
            {
                opt.AddJsonFile("appsettings.json");
                opt.AddCommandLine(args);
            })
            .UseUrls("http://localhost:5000")
                .UseStartup<Startup>()
                .Build();
    }
}
